package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.check.CheckReportDTO;
import com.ehome.poct.core.api.dto.check.TestRecordDetailDTO;
import com.ehome.poct.core.api.dto.check.TestReportDataConfigDTO;
import com.ehome.poct.core.api.dto.checkdata.BldCheckDataDTO;
import com.ehome.poct.core.api.dto.report.ReportItemDTO;
import com.ehome.poct.core.api.dto.report.ReportRequestDTO;
import com.ehome.poct.core.dao.entity.check.TestRecordDetailDO;
import com.ehome.poct.core.dao.mybatis.mapper.TestRecordDetailMapper;
import com.ehome.poct.core.repository.check.TestRecordDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 检测记录RepositoryImpl
 *
 * @author ehome
 * @Date 2022-03-19 16:15
 */
@Repository
public class TestRecordDetailRepositoryImpl extends AbstractCrudRepository<TestRecordDetailDO, TestRecordDetailDTO> implements TestRecordDetailRepository {
    @Autowired
    private TestRecordDetailMapper testRecordDetailMapper;
    private final Integer UNI_TYPE = 121;

    @Override
    public void updateByOrderNo(ReportItemDTO reportItem, ReportRequestDTO reportRequestDTO, List<TestReportDataConfigDTO> dataReportConfigList) {
        //检测人份
        BigDecimal peoplePortion = new BigDecimal("0.00");
        if (UNI_TYPE.equals(reportItem.getStripeCode())) {
            //检测人份
            peoplePortion = dataReportConfigList.stream()
                    .filter(t -> t.getReportCode().equals(reportItem.getReportCode())).findFirst()
                    .map(TestReportDataConfigDTO::getTestPeoplePortion).orElse(null);
        }
        //回写检测记录
        testRecordDetailMapper.update(null, Wrappers.<TestRecordDetailDO>lambdaUpdate().set(TestRecordDetailDO::getTestTime, LocalDateTime.now()).set(TestRecordDetailDO::getTestPeoplePortion2, peoplePortion)
                .set(TestRecordDetailDO::getSno, reportRequestDTO.getTestData().get(0).getSno()).set(TestRecordDetailDO::getTestReportId, reportItem.getReportId())
                .eq(TestRecordDetailDO::getOrderNo, reportRequestDTO.getReportReqDTO().getOrderNo()).eq(TestRecordDetailDO::getStripTypeCode, reportItem.getStripeCode()));
    }

    @Override
    public void updateByOrderNo(CheckReportDTO checkReport, BldCheckDataDTO data) {
        //回写检测记录
        testRecordDetailMapper.update(null, Wrappers.<TestRecordDetailDO>lambdaUpdate().set(TestRecordDetailDO::getTestTime, LocalDateTime.now()).set(TestRecordDetailDO::getTestPeoplePortion2, 0.00)
                .set(TestRecordDetailDO::getSno, data.getSno()).set(TestRecordDetailDO::getTestReportId, checkReport.getBloodCheckReportId())
                .eq(TestRecordDetailDO::getOrderNo, checkReport.getOrderNo()).eq(TestRecordDetailDO::getStripTypeCode, 100));
    }

    @Override
    public void updateUnitReportSno2(TestRecordDetailDTO testRecordDetailDTO) {
        testRecordDetailMapper.update(null, Wrappers.<TestRecordDetailDO>lambdaUpdate()
                .set(TestRecordDetailDO::getTestPeoplePortion2, testRecordDetailDTO.getTestPeoplePortion2())
                .set(TestRecordDetailDO::getSno2, testRecordDetailDTO.getSno2())
                .eq(TestRecordDetailDO::getOrderNo, testRecordDetailDTO.getOrderNo())
                .eq(TestRecordDetailDO::getStripTypeCode, testRecordDetailDTO.getStripTypeCode()));
    }

    @Override
    public void delSaaAndCrpRecord(String orderNo, Integer stripTypeCode) {
        QueryWrapper<TestRecordDetailDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TestRecordDetailDO::getOrderNo, orderNo).eq(TestRecordDetailDO::getStripTypeCode, stripTypeCode);
        testRecordDetailMapper.delete(queryWrapper);
    }

    @Override
    public void updateByOrderNoAndStripTypeCode(TestRecordDetailDTO testRecordDet) {
        TestRecordDetailDO testRecordDetailDO = BeanUtil.copyProperties(testRecordDet, TestRecordDetailDO.class);
        testRecordDetailMapper.updateByOrderNoAndStripTypeCode(testRecordDetailDO, testRecordDetailDO.getOrderNo(), testRecordDetailDO.getStripTypeCode());
    }

    @Override
    public List<TestRecordDetailDTO> findByOrderNoAndStripTypeCodeIn(String orderNo, List<Integer> stripTypeCodeList) {
        List<TestRecordDetailDO> recordDetailDOList = testRecordDetailMapper.findByOrderNoAndStripTypeCodeIn(orderNo, stripTypeCodeList);
        return BeanUtil.copyToList(recordDetailDOList, TestRecordDetailDTO.class);
    }
}
